#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gate.h"
#include "list.h"
#include "queue.h"
#include "stack.h"

void destroy(void *data)
{
	free(data);
}

int match(const void *key1, const void *key2)
{
	return 1;
}

int main(int argc, char *argv[])
{
	int res = gate_init();
	
	if(1 == res)
	{
		printf("gate init success\n");
	}
	else
	{
		printf("gate init failed\n");
		return 1;
	}
	
	struct gate *gate_p = gate_get();
	//char* data = "hello world!";
	
	gate_p->num = 9;
	gate_p->data = "ssssssssssaa";//data;

	printf("from io.c\n");
	
	printf("gate info:\ngate.num: %d\ngate.data: %s\n", gate_p->num, gate_p->data);
	
	//free(gate_p);
	
	printf("-----------list test-----------\n\n");
	struct io_list *list = NULL;
	list = (struct io_list*)malloc(sizeof(struct io_list));
	
	io_list_init(list, destroy, match);
	io_list_ins_next(list, io_list_head(list), (void*)gate_p);
	
	printf("list info:\nsize:%d\nhead:%p\ntail:%p\n", io_list_size(list), io_list_head(list), io_list_tail(list));
	
	io_list_destroy(list);
	
	printf("-----------queue test-----------\n\n");
	io_queue *queue = NULL;
	queue = (io_queue*)malloc(sizeof(io_queue));
	
	/*
	char *str1 = (char*)malloc(13 * sizeof(char));
	strncpy(str1, "queue-data-1", 13);
	char *str2 = (char*)malloc(13 * sizeof(char));
	strncpy(str2, "queue-data-2", 13);
	char *str3 = (char*)malloc(13 * sizeof(char));
	strncpy(str3, "queue-data-3", 13);
	*/
	
	io_queue_init(queue, destroy, match);
	io_queue_enqueue(queue, "queue-data-1");//(void*)str1);
	io_queue_enqueue(queue, "queue-data-2");//(void*)str2);
	io_queue_enqueue(queue, "queue-data-3");//(void*)str3);
	
	void *data = NULL;
	while(io_queue_size(queue) > 0)
	{
		data = NULL;
		io_queue_dequeue(queue, (void**)&data);
		printf(">>>%p -- %s\n", (void*)data, (char*)data);
	}
	
	printf("-----------stack test-----------\n\n");
	io_stack *stack = NULL;
	stack = (io_stack*)malloc(sizeof(io_stack));
	
	io_stack_init(stack, destroy, match);
	io_stack_push(stack, "stack-data-1");
	io_stack_push(stack, "stack-data-2");
	io_stack_push(stack, "stack-data-3");
	
	while(io_stack_size(stack) > 0)
	{
		data = NULL;
		io_stack_pop(stack, (void**)&data);
		printf(">>>%p -- %s\n", (void*)data, (char*)data);
	}
	
	return 0;
}
